'use strict';
app.controller('advertisementCtrl',['$scope','utilService','advertisementService','toaster','$modal', function($scope,utilService,advertisementService,toaster,$modal){
    $scope.advertise = {};
    // call server.
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'advertise',
            params:$scope.advertise
        }).then(function(result){
        });
    }
    $scope.query();

    //禁用启用
    $scope.changeStatus= function(id){
        $scope.advertise = {
            id:id
        };
        advertisementService.changeStatus($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.query();
                    break;
                case 634:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }

    //删除广告
    $scope.openDelete = function (id) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/advertisement/advertisement_delete.html',
            controller: 'delAdvertisemenlCtrl',
            scope: $scope,
            size:'sm',
            resolve: {
                data: function () {
                    var data = {};
                    data.id = id;
                    return data;
                }
            }
        });
    }
}]);

//广告详情Controller
app.controller('advertisementDetailCtrl',['$scope','$state','$stateParams','advertisementService','toaster','$filter', function($scope,$state,$stateParams,advertisementService,toaster,$filter){
    $scope.advertise = {
        id:$stateParams.id
    };
    //根据id查询广告
    advertisementService.findById($scope).then(function(result){
        switch (result.status){
            case 200:
                $scope.advertise = result.data;
                break;
            case 634:
                toaster.pop('waiting', "提示", result.message);
                break;
            default :
                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                break
        }
    });
}]);

//查询广告分类下拉框controller
app.controller('advertisementTypeSelectCtrl',['$scope','advertisementService','toaster', function($scope,advertisementService,toaster){
    advertisementService.getList($scope).then(function (result) {
        switch (result.status) {
            case 200:
                $scope.selectList = result.data;
                break;
            default :
                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                break
        }
    });
}]);

//删除模态框Controller
app.controller('delAdvertisemenlCtrl', ['$scope', '$modalInstance','$state','advertisementService','toaster','data',
    function($scope, $modalInstance,$state,advertisementService,toaster,data) {
        $scope.advertise=data;

        $scope.AllowToDelete=true;

        //确认删除
        $scope.ok=function(){
            if(!$scope.AllowToDelete){
                return;
            }
            $scope.AllowToDelete=false;
            advertisementService.delAdvertise($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");;
                        $scope.AllowToDelete=true;
                        $scope.query();
                        break;
                    case 634:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.AllowToDelete=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToDelete=true;
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);
//广告添加controller
app.controller('advertisementAddCtrl',['$scope','$state','advertisementService','$stateParams','toaster',
    function($scope,$state,advertisementService,$stateParams,toaster) {
        advertisementService.getList($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.selectList = result.data;
                    break;
                case 633:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
        //新增广告
        $scope.AllowToAdd=true;
        $scope.addAdvertise = function(){
            if(!$scope.AllowToAdd){
                return;
            }
            $scope.AllowToAdd=false;
            $scope.advertise.imgUrl=advertisementService.fileName;
            advertisementService.addAdvertise($scope).then(function(result){
                switch (result.status){
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $scope.AllowToAdd=true;
                        $state.go('app.advertisement');
                        break;
                    default:
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToAdd=true;
                        break
                }
            });
        }
    }
]);

//广告修改controller
app.controller('advertisementEditCtrl',['$scope','advertisementService','$stateParams','toaster','$state',
    function($scope,advertisementService,$stateParams,toaster,$state) {
        $scope.advertise = {
            id:$stateParams.id
        };
        //根据id查询问题
        advertisementService.findById($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.advertise = result.data;
                    if($scope.advertise.imgUrl!=null){
                        $scope.$broadcast('hasPicture',true);
                    };
                    break;
                case 634:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
        advertisementService.getList($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.selectList = result.data;
                    break;
                case 633:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
        //修改广告
        $scope.AllowToEdit=true;
        $scope.editAdvertise = function() {
            if(!$scope.AllowToEdit){
                return;
            }
            $scope.AllowToEdit=false;
            if ("undefined" != typeof advertisementService.fileName) {
                $scope.advertise.imgUrl = advertisementService.fileName;
            }
            advertisementService.editAdvertise($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $scope.AllowToEdit=true;
                        $state.go('app.advertisement');
                        break;
                    case 634:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.AllowToEdit=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToEdit=true;
                        break
                }
            });
        }
        //删除图片路径
        $scope.delAdvertisePic=function(){
            advertisementService.delAdvertisePic($scope).then(function(result){
                switch (result.status){
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $scope.$broadcast('hasPicture',false);
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break
                }
            });
        }
    }
]);

/*上传图片controller*/
app.controller('fileUploadCtrl', ['$scope', 'FileUploader','$cookies','toaster','fileUploadService', 'advertisementService',function($scope, FileUploader,$cookies,toaster,fileUploadService,advertisementService) {
    $scope.uploadStatus = $scope.uploadStatus1 = false;
    $scope.queueLimit=1;
    $scope.hasPicture=false;

    $scope.$on("hasPicture",function (event, msg) {
        $scope.hasPicture = msg;
    });

    var uploader = $scope.uploader = new FileUploader({
        url: $scope.app.domain+'/upload',
        formData:[{sessionId:$cookies.sessionId}],
        queueLimit:$scope.queueLimit
    });

    var status=false;
    // FILTERS
    uploader.filters.push({
        name: 'picFilter',
        fn: function(item, options) {
            if(item.size>2*1024*1024){
                toaster.pop('waiting', "添加图片失败", "文件太大,超出2M大小");
                return false;
            }
            if(!(item.type=='image/png' ||item.type=='image/gif' || item.type=='image/jpeg' || item.type=='image/jpg')){
                toaster.pop('waiting', "添加图片失败", "不属于图片类型");
                return false;
            }

            if(this.queue.length > $scope.queueLimit){
                toaster.pop('waiting', "添加图片失败", "超出图片上传数量");
                return false;
            }

            return true;
        }
    });

    // CALLBACKS
    uploader.onWhenAddingFileFailed = function(item,filter, options) {
        console.info('添加文件失败', item, filter, options);
    };
    uploader.onAfterAddingFile = function(fileItem) {
        if(this.queue.length > $scope.queueLimit){
            toaster.pop('waiting', "添加图片失败", "超出图片上传数量");
            return false;
        }
    };

    uploader.onAfterAddingAll = function(addedFileItems) {
        if(!uploader.getNotUploadedItems().length){
            return;
        }
        uploader.uploadAll();
    };
    uploader.onBeforeUploadItem = function(item) {

    };
    uploader.onProgressItem = function(fileItem, progress) {
        console.info('上传一个文件中...', fileItem, progress);
    };
    uploader.onProgressAll = function(progress) {
        console.info('上传所有文件中...', progress);
    };
    uploader.onSuccessItem = function(fileItem, response, status, headers) {
        console.info('上传成功的文件', fileItem, response, status, headers);
        switch (status){
            case 200:
                advertisementService.fileName=response.data;
                fileItem.fileName=response.data;
                fileItem.imgsrc=$scope.app.image.temp+response.data;
                break;
            default :
                break;
        }

    };

    uploader.onErrorItem = function(fileItem, response, status, headers) {
        console.info('上传失败的文件', fileItem, response, status, headers);
        toaster.pop('waiting', "上传图片失败", "未知错误");
    };

    uploader.onCancelItem = function(fileItem, response, status, headers) {
        console.info('取消上传的文件', fileItem, response, status, headers);
    };

    uploader.onCompleteItem = function(fileItem, response, status, headers) {
        console.info('完成上传的文件', fileItem, response, status, headers);
    };

    uploader.onCompleteAll = function() {
        console.info('所有文件上传完成');
    };

    $scope.del=function(item){
        fileUploadService.del($scope,item).then(function(result){
            switch (result.status){
                case 200:
                    item.remove();
                    break;
                default :
                    break;
            }
        });
    }

    //监听父容器广播的数据
    $scope.$on('goodsImages',function(e,msg){
        //获取到父控制器广播的内容；
        $scope.hasPicture=msg;
    });

}]);




